﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    public partial class ModificationPersonalInformation : Form
    {
   
        /// <summary>
        /// 参数传递
        /// </summary>
        /// <param name="RealName">姓名</param>
        /// <param name="GradeName">年级</param>
        /// <param name="UserName">帐号</param>
        /// <param name="PassWord">密码</param>
        /// <param name="IdentityCard">身份证</param>
        /// <param name="Sex">性别</param>
        /// <param name="Birthday">生日</param>
        public ModificationPersonalInformation(string RealName, string GradeName, string UserName, string PassWord, string IdentityCard, string Sex, DateTime Birthday)
        {
            InitializeComponent();
            bindCbox();


            txt_UserName.Text = UserName;
            txt_PassWord.Text = PassWord;
            txt_IdentityCard.Text = IdentityCard;
            txt_RealName.Text = RealName;
            if (Sex=="男")
            {
                rdo_Man.Checked=true;
            }
            if (Sex=="女")
            {
                rdo_Woman.Checked = true;
            }
            cbo_GradeName.Text = GradeName;
            this.cbo_Years.SelectedValue = Birthday.Year;
            this.cbo_Months.SelectedValue = Birthday.Month;
            this.cbo_Days.SelectedValue = Birthday.Day;
        }

        SIMS_GUI_Item_DataBase db = new SIMS_GUI_Item_DataBase();
        private void abb_Load(object sender, EventArgs e)
        {
            this.txt_UserName.Enabled = false;//禁用文本输入框
        }

        //判断闰年 
        private bool checkleap(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
                return true;
            else return false;
        }

        //绑定每月的天数 
        private void binddays(int year, int month)
        {
            int i;
            DataTable dt3 = new DataTable();
            dt3.Columns.Add("id");
            dt3.Columns.Add("value");
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    for (i = 1; i <= 31; i++)
                    {
                        var dr = dt3.NewRow();
                        dr[0] = i;
                        dr[1] = i;
                        dt3.Rows.Add(dr);
                    }
                    break;
                case 2:
                    if (checkleap(year))
                    {
                        for (i = 1; i <= 29; i++)
                        {
                            var dr = dt3.NewRow();
                            dr[0] = i;
                            dr[1] = i;
                            dt3.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        for (i = 1; i <= 28; i++)
                        {
                            var dr = dt3.NewRow();
                            dr[0] = i;
                            dr[1] = i;
                            dt3.Rows.Add(dr);
                        }
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    for (i = 1; i <= 30; i++)
                    {
                        var dr = dt3.NewRow();
                        dr[0] = i;
                        dr[1] = i;
                        dt3.Rows.Add(dr);
                    }
                    break;
            }
            this.cbo_Days.DisplayMember = "value";
            this.cbo_Days.ValueMember = "id";
            this.cbo_Days.DataSource = dt3;
        }

        private void cbo_Years_SelectedIndexChanged(object sender, EventArgs e)
        {
            int year, month;
            year = Int32.Parse(this.cbo_Years.SelectedValue.ToString());
            month = 1;
            binddays(year, month);//绑定年月
        }

        private void cbo_Months_SelectedIndexChanged(object sender, EventArgs e)
        {
            int year, month;
            year = Int32.Parse(this.cbo_Years.SelectedValue.ToString());
            month = Int32.Parse(this.cbo_Months.SelectedValue.ToString());
            binddays(year, month);//绑定年月
        }

        /// <summary>
        /// 确定更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            string UserName = txt_UserName.Text;
            string PassWord = txt_PassWord.Text;
            string RealName = txt_RealName.Text;
            string IdentityCard = txt_IdentityCard.Text;
            string Sex = "女";
            if (rdo_Man.Checked == true)
            {
                Sex = "男";
            }
            if (rdo_Woman.Checked)
            {
                Sex = "女";
            }

            string GradeName = cbo_GradeName.Text;

            int GradeId = 0;
            if (GradeName == "大一")
            {
                GradeId = 1;
            }
            if (GradeName == "大二")
            {
                GradeId = 2;
            }
            if (GradeName == "大三")
            {
                GradeId = 3;
            }

            var dte = Convert.ToDateTime(string.Format("{0}-{1}-{2}", cbo_Years.Text.Trim(),
                                                                      cbo_Months.Text.Trim(),
                                                                      cbo_Days.Text.Trim()));
            string sql = string.Format(@"
                                         update UserInformation set RealName='{0}';
                                         update UserInformation set IdentityCard='{1}';
                                         where UserName=1723003097",RealName, IdentityCard);
            /*
             *                           update UserInformation set UserName='{0}';
                                         update UserInformation set PassWord='{1}';
                                         update UserInformation set RealName='{2}';
                                         update UserInformation set IdentityCard='{3}';
                                         update UserInformation set Sex='{4}';
                                         update UserInformation set GradeId='{5}';
                                         update UserInformation set Birthday='{6}'*/
            int number = Convert.ToInt32(db.ExecNonQuery(sql));
            if (number > 0)
            {
                MessageBox.Show("修改失败");
            }
            else
            {
                MessageBox.Show("修改成功");
            }
        }
        private void bindCbox()
        {
            //绑定年
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("id");
            dt1.Columns.Add("value");
            int i;
            for (i = 1900; i <= 2099; i++)
            {
                var dr = dt1.NewRow();
                dr[0] = i;
                dr[1] = i;
                dt1.Rows.Add(dr);
            }
            this.cbo_Years.DisplayMember = "value";
            this.cbo_Years.ValueMember = "id";
            this.cbo_Years.DataSource = dt1;

            //绑定月
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("id");
            dt2.Columns.Add("value");
            for (i = 1; i <= 12; i++)
            {
                var dr = dt2.NewRow();
                dr[0] = i;
                dr[1] = i;
                dt2.Rows.Add(dr);
            }
            this.cbo_Months.DisplayMember = "value";
            this.cbo_Months.ValueMember = "id";
            this.cbo_Months.DataSource = dt2;
            int year, month;
            year = Int32.Parse(this.cbo_Years.SelectedValue.ToString());
            month = Int32.Parse(this.cbo_Months.SelectedValue.ToString());
            binddays(year, month);//绑定天 
        }
    }
}
